package com.example.owner.factory;

import com.example.owner.aop.ProductBean;
import com.example.owner.bean.impl.Product;
import io.micronaut.aop.InterceptorBean;
import io.micronaut.aop.MethodInterceptor;
import io.micronaut.context.annotation.Factory;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Factory
public class AopFactory {

    @InterceptorBean(ProductBean.class)
    MethodInterceptor<Product, Object> aroundInvoke() {
        log.info("AopFactory >> aroundInvoke");
        return context -> {
            final Product product = context.getTarget();

            log.info("AopFactory >> aroundInvoke {} , {}", product, context.getKind());
            switch (context.getKind()) {
                case POST_CONSTRUCT: // (2)
                    product.setActive(true);
                    return context.proceed();
                case PRE_DESTROY: // (3)
                    product.setActive(false);
//                    productService.removeProduct(product);
                    return context.proceed();
                default:
                    return context.proceed();
            }
        };
    }
}
